package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: lx
 * @Time: 15:26
 * @Description:
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ListService listService;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//高级别的客户端
    //@Autowired
    //private ElasticsearchTemplate elasticsearchTemplate;//低级别
    //Mapping
    @GetMapping("/mappings")
    public Result mappings(){
        //创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //映射表关联关系
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }


    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId") Long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(name = "skuId") Long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }
    //商品热度更新
    @GetMapping("/inner/hotScore/{skuId}")
    public Result hotScore(@PathVariable(name = "skuId") Long skuId){

        listService.hotScore(skuId);
        return Result.ok();
    }

    //开始搜索  http://.... /api/list
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);
    }




}
